;/*==================================================================================================
;  Description   Main
;  
;  20080905      Wim       Created
;==================================================================================================*/
#include general.ahk

SetMouseDelay, 75
HotKey, ^+1, Start
HotKey, ^+2, Stop
HotKey, ^+q, Quit
HotKey, ^+x, Quit
HotKey, ^+r, DoReload
return


;/*==================================================================================================
;  Description
;  
;  20080905      Wim       Created
;==================================================================================================*/
Quit:
  ExitApp
  return


;/*==================================================================================================
;  Description
;  
;  20080905      Wim       Created
;==================================================================================================*/
Start:
  osd := "Ctrl-Alt 1:start, 2:stop, q:quit, r:reload."
  delta := 3
  sleeper := 0
  outX := 0
  outY := 0
  state := 0
  running := 1
  state := "start"
  
  SplashTextOn, 400, 50, Diva Script, ods
  WinMove, Diva Script, , 0, 0

  ControlSetText, Static1, %osd%`n Waiting for Diva Web Pages, Diva Script
  WinWait, Diva Web -, 
  IfWinNotActive, Diva Web -, , 
  {
    ControlSetText, Static1, %osd%`n Activating Diva Web Pages, Diva Script
    WinActivate, Diva Web -,
  }
  ControlSetText, Static1, %osd%`n Waiting for Active Diva Web Pages, Diva Script
  WinWaitActive, Diva Web -, 

  loop
  {
    ;should select correct application first!
    if (state = "start")
    {
      deltaY := getOffset()
      state := "selectlive"
    }
    else if (state = "selectlive")
    {
      ;move mouse to live and click
      ControlSetText, Static1, %osd%`n '%state%', Diva Script
      MouseClickRandomPath(1067,  deltaY + 252, delta)
      ;Sleep, 500 not needed, because we 'slowly' move to next pos
      state := "livecampos"
      sleeper := 0
    }
    else if (state = "livecampos")
    {
      ;slowly move to camera click position
      ControlSetText, Static1, %osd%`n '%state%', Diva Script
      MouseMoveRandomPath(1077,  deltaY + 440, delta)
      state := "waitforlive"
    }
    else if (state = "waitforlive")
    {
      ;wait for live page to be completed by looking for a colored pixel
      ControlSetText, Static1, %osd%`n '%state% (%sleeper%)', Diva Script
      sleeper++
      PixelSearch, outX, outY, 593, deltaY + 879, 599, deltaY + 895, 0x613434, 0, Fast RGB
      if (outX > 0)
      {
        ;found, next state
        state := "selectlivecam"
      }
      else if (sleeper > 100)
      {
        ;timeout, reset state
        state := "selectlive"
      }
      else
      {
        ;wait
        Sleep, 100
      }
    }
    else if (state = "selectlivecam")
    {
      ;directly click the mouse, we've moved here slowly
      ControlSetText, Static1, %osd%`n '%state%', Diva Script
      MouseClickRandom(1077,  deltaY + 440, delta)
      state := "getliveimages"
      sleeper := 20
    }
    else if (state = "getliveimages")
    {
      ControlSetText, Static1, %osd%`n '%state% (%sleeper%)', Diva Script
      Sleep, 100
      sleeper--
      if (sleeper <= 0)
      {
        state := "selectplay"
      }
    }
    else if (state = "selectplay")
    {
      ControlSetText, Static1, %osd%`n '%state%', Diva Script
      MouseClickRandomPath(1063, deltaY + 266, delta)
      Sleep, 500
      state := "playcampos"
      sleeper := 0
    }
    else if (state = "playcampos")
    {
      ;move mouse to next pos
      ControlSetText, Static1, %osd%`n '%state%', Diva Script
      MouseMoveRandomPath(1077, deltaY + 440, delta)
      state := "waitforplay"
    }
    else if (state = "waitforplay")
    {
      ;wait for play page to be loaded
      ControlSetText, Static1, %osd%`n '%state% (%sleeper%)', Diva Script
      sleeper ++
      PixelSearch, outX, outY, 593, deltaY + 872, 596, deltaY + 877, 0x346134, 0 , Fast RGB
      if (outX > 0)
      {
        state := "selectplaycam"
      }
      else if (sleeper > 100)
      {
        state := "selectplay"
      }
      else
      {
        Sleep, 100
      }
    }
    else if (state = "selectplaycam")
    {
      ;directly select camera
      ControlSetText, Static1, %osd%`n '%state%', Diva Script
      MouseClickRandom(1077, deltaY + 440, delta)
      state := "startplay"
      sleeper := 20
    }
    else if (state = "startplay")
    {
      ;slowly goto and click start play
      ControlSetText, Static1, %osd%`n '%state%', Diva Script
      MouseClickRandomPath(627, deltaY + 919, delta)
      state := "getplayimages"
    }
    else if (state = "getplayimages")
    {
      ;wait for frames and next state (reset to beginning)
      ControlSetText, Static1, %osd%`n '%state% (%sleeper%)', Diva Script
      Sleep, 100
      sleeper--
      if (sleeper <= 0)
      {
        state := "end"
      }
    }
    else
    {
      state := "start"
    }
    
    if (running = 0)
      break
  }
  SplashTextOff
  return


;/*==================================================================================================
;  Description
;  
;  20080905      Wim       Created
;==================================================================================================*/
Stop:
  running := 0
  return


getOffset()
{
  result := 0
  PixelGetColor, pixcolor, 1058, 251, RGB
  if (pixcolor = 0x212121)
    result := 10
  ;MsgBox, %pixcolor%, %result%
  return result 
}

DoReload:
  Reload
  Sleep 1000 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
;  MsgBox, 4,, The script could not be reloaded. Would you like to open it for editing?
;  IfMsgBox, Yes, Edit
  return
